/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hualu.zks.GUI;

import com.hualu.zks.CommonUtils.Globalparameters;
import com.hualu.zks.CommonUtils.ZKSPreferences;
import com.hualu.zks.Model.User;
import com.hualu.zks.Dao.ConnDb;
import com.hualu.zks.Dao.UserDao;
import com.hualu.zks.GUI.MainWindow;
import com.jtattoo.plaf.smart.SmartLookAndFeel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Administrator
 */
public class UserLogin extends javax.swing.JFrame {

    ZKSPreferences lgp;
    UserDao userDao = new UserDao();

    public UserLogin() {
//        UserLogin.setDefaultLookAndFeelDecorated(true);
//        try {
//            SmartLookAndFeel.setTheme();
//            SmartLookAndFeel lookFeel = new SmartLookAndFeel();
//            javax.swing.UIManager.setLookAndFeel(lookFeel);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        initComponents();

        lgp = new ZKSPreferences(this);
        username.setText(lgp.getUserName());
//        password.setText(lgp.getUserPwd());
        password.setText("admin");
        
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        loginGrp = new javax.swing.ButtonGroup();
        name = new javax.swing.JLabel();
        pass = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        loginBtn = new javax.swing.JButton();
        title = new javax.swing.JLabel();
        quitBtn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(229, 229, 229));
        setMaximumSize(new java.awt.Dimension(720, 576));
        setName("login"); // NOI18N
        setPreferredSize(new java.awt.Dimension(720, 576));
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        name.setFont(new java.awt.Font("微软雅黑", 0, 25)); // NOI18N
        name.setText("用户名：");

        pass.setFont(new java.awt.Font("微软雅黑", 0, 25)); // NOI18N
        pass.setText("密   码：");
        pass.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        username.setFont(new java.awt.Font("微软雅黑", 0, 30)); // NOI18N
        username.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                usernameKeyPressed(evt);
            }
        });

        password.setFont(new java.awt.Font("微软雅黑", 0, 30)); // NOI18N
        password.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                passwordKeyPressed(evt);
            }
        });

        loginBtn.setFont(new java.awt.Font("微软雅黑", 0, 25)); // NOI18N
        loginBtn.setText("登录");
        loginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginBtnActionPerformed(evt);
            }
        });
        loginBtn.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                loginBtnKeyPressed(evt);
            }
        });

        title.setFont(new java.awt.Font("微软雅黑", 1, 35)); // NOI18N
        title.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        title.setText("欢迎中控系统");

        quitBtn.setFont(new java.awt.Font("微软雅黑", 0, 25)); // NOI18N
        quitBtn.setText("取消");
        quitBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                quitBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(212, 212, 212)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(loginBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(129, 129, 129)
                        .addComponent(quitBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(pass)
                            .addComponent(name))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(username)
                            .addComponent(password)))
                    .addComponent(title, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(247, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addComponent(title, javax.swing.GroupLayout.PREFERRED_SIZE, 61, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(name))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(pass))
                .addGap(95, 95, 95)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginBtn)
                    .addComponent(quitBtn))
                .addGap(92, 92, 92))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void loginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loginBtnActionPerformed
        // TODO add your handling code here:连接数据库查询用户名密码
        String passwordstring = String.valueOf(password.getPassword());
        if (passwordstring.matches("[A-Za-z0-9_]+")) {
            if (username.getText().matches("[\\u4e00-\\u9fa5A-Za-z0-9_]+")) {
                //-1 数据库连接失败 0用户名校验失败 1用户名校验成功
                Globalparameters.loginUser = userDao.checkUserLogin(username.getText(), passwordstring);

                if (Globalparameters.loginUser.getLoginInfo() == "0") {
                    this.setVisible(false);
                                new MainWindow().setVisible(true);
                    lgp.setUserName(username.getText());
                    password.setText("");
                    loginBtn.setEnabled(true);
//                                new testicon().setVisible(true);

//                                lgp.setUserPwd(password.getText());
//                                if (serverBtn.isSelected()) {
//                                   lgp.setSDBIP(DBIP.getText());
//                                   lgp.setSDBPort(DBPort.getText());
//                                   lgp.setPCBtn("0");
//                                   localflag = false;
//                                }
//                                if (PCBtn.isSelected()) {
//                                   lgp.setPDBIP(DBIP.getText());
//                                   lgp.setPDBPort(DBPort.getText());
//                                   lgp.setPCBtn("1");
//                                   localflag = true;
//                                }
                } else if (Globalparameters.loginUser.getLoginInfo() == "-2") {
                    loginBtn.setEnabled(true);
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！", "警告", JOptionPane.WARNING_MESSAGE);
                    username.requestFocus();
                } else {
                    loginBtn.setEnabled(true);
                    System.out.println("database connect failed!!!");
                }
//                        } else {
//                            loginBtn.setEnabled(true);
//                            JOptionPane.showMessageDialog(null, "端口号输入错误!", "警告", JOptionPane.WARNING_MESSAGE);
//                        }
//                    } else {
//                        loginBtn.setEnabled(true);
//                        JOptionPane.showMessageDialog(null, "端口号输入错误!", "警告", JOptionPane.WARNING_MESSAGE);
//                    }
            } else {
                loginBtn.setEnabled(true);
                JOptionPane.showMessageDialog(null, "用户名或密码错误!", "警告", JOptionPane.WARNING_MESSAGE);
            }
        } else {
            loginBtn.setEnabled(true);
            JOptionPane.showMessageDialog(null, "用户名或密码错误!", "警告", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_loginBtnActionPerformed

    private void quitBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_quitBtnActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_quitBtnActionPerformed

    private void usernameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_usernameKeyPressed
        if(evt.getKeyChar()==KeyEvent.VK_ENTER){
                    System.err.println(11111111);
                    loginBtn.doClick();
                }
    }//GEN-LAST:event_usernameKeyPressed

    private void passwordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_passwordKeyPressed
        if(evt.getKeyChar()==KeyEvent.VK_ENTER){
                    System.err.println(11111111);
                    loginBtn.doClick();
                }
    }//GEN-LAST:event_passwordKeyPressed

    private void loginBtnKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_loginBtnKeyPressed
       if(evt.getKeyChar()==KeyEvent.VK_ENTER){
                    System.err.println(11111111);
                    loginBtn.doClick();
                }
    }//GEN-LAST:event_loginBtnKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UserLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
//                try {
//            UIManager.setLookAndFeel(new com.jtattoo.plaf.aero.AeroLookAndFeel());
//            UIManager.setLookAndFeel(new javax.swing.plaf.metal.MetalLookAndFeel());
//            UIManager.getSystemLookAndFeelClassName();
                //              UIManager.setLookAndFeel(new com.jtattoo.plaf.luna.LunaLookAndFeel());
                ///每个皮肤包都有一个****LookAndFeel类，查下文档或看下JAR都能找到，在这里改下就可以了
//        } catch (UnsupportedLookAndFeelException ex) {
//        }

                new UserLogin().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton loginBtn;
    private javax.swing.ButtonGroup loginGrp;
    private javax.swing.JLabel name;
    private javax.swing.JLabel pass;
    private javax.swing.JPasswordField password;
    private javax.swing.JButton quitBtn;
    private javax.swing.JLabel title;
    private javax.swing.JTextField username;
    // End of variables declaration//GEN-END:variables
}
